const app = getApp()
const constant = require("../../../../constant.js")
const storage = require("../../../../utils/storage.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,

    animationData: {},
    animationPickerMenu: {},
    pickerMenuIsShow: false,
    value: [0],
    disabled: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      host: constant.api(app.globalData.pord).url,
      mobile: wx.getStorageSync(storage.keys.mobile),
      courseRoundId: options.courseRoundId
    })

    // 初始化动画变量
    var animation = wx.createAnimation({
      duration: 500,
      transformOrigin: "50% 50%",
      timingFunction: 'ease',
    })
    this.animation = animation;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: "正在加载中..."
    })

    var that = this;

    wx.request({
      url: that.data.host + '/coach/course-rounds/' + that.data.courseRoundId + '/fitness-group?mobile=' + that.data.mobile,
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.code == "0") {
          that.setData({
            groupName: res.data.data.originalGroup
          });

          var groups = [];
          var groupNames = [];

          if (res.data.data.fitnessGroups.length > 0) {
            for (var i = 0; i < res.data.data.fitnessGroups.length; i++) {
              groups.push({
                id: res.data.data.fitnessGroups[i].id,
                name: res.data.data.fitnessGroups[i].name
              });
              groupNames.push(res.data.data.fitnessGroups[i].name);
            }
          }

          that.setData({
            groups: groups,
            groupNames: groupNames
          })

          wx.hideLoading();
        } else {
          wx.hideLoading();

          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: function (res) {
        wx.hideLoading();

        wx.showToast({
          title: '请求数据失败',
          icon: 'none',
          duration: 1000
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value,
      groupId: this.data.groups[e.detail.value].id
    })
  },

  changeGroup: function () {
    var that = this;

    if (!that.data.groupId) {
      wx.showToast({
        title: '请选择分组',
        icon: 'none',
        duration: 1000
      });
      return;
    } else {
      that.setData({
        disabled: true
      })
      wx.request({
        url: that.data.host + '/coach/course-rounds/' + that.data.courseRoundId + '/edit-fitness-group?mobile=' + that.data.mobile,
        method: 'POST',
        data: {
          fitness_group_id: that.data.groupId
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if (res.data.code == "0") {
            wx.showToast({
              title: res.data.message,
              icon: 'success',
              duration: 1000,
              mask: true,
              success: function () {
                setTimeout(function () {
                  wx.navigateBack({
                    delta: 1
                  })
                }, 1000)
              }
            });
          } else {
            that.setData({
              disabled: false
            })
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 3000,
              mask: true
            })
          }
        },
        fail: function (res) {
          that.setData({
            disabled: false
          })
          wx.showToast({
            title: '保存失败',
            icon: 'none',
            duration: 3000,
            mask: true
          })
        }
      });
    }
  },
  bindPicker: function () {
    var that = this
    if (that.data.pickerMenuIsShow) {
      return
    }
    that.startPickerAnimation(true)
  },
  // 执行动画
  startPickerAnimation: function (isShow) {
    var that = this
    if (isShow) {
      if(!that.data.groupId) {
        that.setData({
          index: 0,
          groupId: that.data.groups[0].id
        })
      }
      that.animation.translateY(0 + 'vh').step()
    } else {
      that.animation.translateY(40 + 'vh').step()
    }
    that.setData({
      animationPickerMenu: that.animation.export(),
      pickerMenuIsShow: isShow,
    })
  },
  // 点击地区选择取消按钮
  cityCancel: function (e) {
    this.startPickerAnimation(false)
  },
  // 点击地区选择确定按钮
  citySure: function (e) {
    var that = this
    that.startPickerAnimation(false)
    
    // this.setData({
    //   index: this.data.currentIndex,
    //   groupId: this.data.currentGroupId
    // })
  },
})